import { Rect } from '../geometry/rect';
import { InkSettings } from './inksetting';

export class ImageItem {
    private _url: string;
    private _image: HTMLImageElement | null;
    private _imageWidth = 0;
    private _imageHeight = 0;
    private _imageLevel = 1;
    private _bounds: Rect;
    private _order: number;
    private _isError: boolean;
    private _error: any;
    private _currentAngle: number = 0;
    private _scaleLevel: number = 1;
    private _brightlightValue: number = 1;
    private _scaleSeed: number = InkSettings.DefaultScaleSeed;
    private _minScale: number = InkSettings.DefaultMinScale;
    private _brightnessSeed: number = InkSettings.DefaultBrightnessSeed;
    private _minBrightness: number = InkSettings.DefaultMinBrightness;
    /**
     * 构造函数
     * @param url 图片url
     * @param order 图片索引
     */
    constructor(url: string, order: number) {
        this._url = url;
        this._order = order;
    }
    /**获取图片url */
    get url() {
        return this._url;
    }
    /**获取图片索引 */
    get order() {
        return this._order;

    }
    /**获取img元素 */
    get image() {
        return this._image;
    }
    /**获取图片实际宽度,单位:像素 */
    get width() {
        return this._imageWidth;
    }
    /**获取图片实际高度,单位:像素 */
    get height() {
        return this._imageHeight;
    }
    /**获取图片宽高比例值 */
    get level() {
        return this._imageLevel;
    }
    /**获取加载图片是否出错 */
    get isError() {
        return this._isError;
    }
    /**获取加载图片的错误信息 */
    get error() {
        return this._error;
    }
    /**获取或设置图片呈现范围 */
    get bounds() {
        return this._bounds;
    }
    set bounds(value: Rect) {
        this._bounds = value;
    }
    /**获取或设置当前对图片应用的缩放值 */
    get currentScale(): number {
        return this._scaleLevel;
    }
    set currentScale(value: number) {
        if (this._scaleLevel !== value) {
            this._scaleLevel = value;
        }
    }
    /**获取或设置当前旋转角度 */
    get currentAngle(): number {
        return this._currentAngle;
    }
    set currentAngle(value: number) {
        if (this._currentAngle !== value) {
            this._currentAngle = value;
        }
    }
    /**获取或设置当前图片应用的亮度值 */
    get currentBrightness(): number {
        return this._brightlightValue;
    }
    set currentBrightness(value: number) {
        if (this._brightlightValue !== value) {
            this._brightlightValue = value;
        }
    }
    public imageRotate(angle: number, callback?: () => void) {
        let curAngle = this.currentAngle;
        curAngle += angle;
        curAngle = curAngle % 360;
        if (this.currentAngle !== curAngle) {
            if (this._image) {
                const canvas = document.createElement('canvas');
                const context = canvas.getContext('2d');
                if (context) {
                    canvas.width = this.width;
                    canvas.height = this.height;
                    context.rotate(curAngle * Math.PI / 180);
                    context.drawImage(this._image, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
                    const url = canvas.toDataURL('image/jpeg');
                    // const a = document.createElement('a');
                    // a.href = url;
                    // a.download = '下载.jpg';
                    // a.click();
                    const _callback = (error: any, image?: HTMLImageElement) => {
                        if (!error && image) {
                            this._image = image;
                            this._imageWidth = image.naturalWidth;
                            this._imageHeight = image.naturalHeight;
                            this._imageLevel = this._imageWidth / this._imageHeight;
                            // 回调处理
                            if (callback) {
                                this.currentAngle = curAngle;
                                callback();
                            }
                        }
                    };
                    this.innerLoadImageAsync(url, _callback);
                }
            }
        }
        
    }
    /**提高图片亮度 */
    public brightLightUp(callback?: () => void) {
        let brightness = this.currentBrightness;
        brightness += this._brightnessSeed;
        if (this.currentBrightness !== brightness) {
        this.currentBrightness = brightness;
            // 回调处理
            if (callback) {
                callback();
            }
        }
    }
    /**降低图片亮度 */
    public brightLightDown(callback?: () => void) {
        let brightness = this.currentBrightness;
        brightness -= this._brightnessSeed;
        if (brightness <= this._minBrightness) {
            brightness = this._minBrightness;
        }
        if (this.currentBrightness !== brightness) {
            this.currentBrightness = brightness;
            // 回调处理
            if (callback) {
                callback();
            }
        }
    }
    /**放大图片 */
    public scaleOut(): number {
        let zoomLevel = this.currentScale;
        zoomLevel += this._scaleSeed;
        this.currentScale = zoomLevel;
        return 1 + this._scaleSeed;
    }
    /**缩小图片 */
    public scaleIn() {
        let ralateScale = 1 - this._scaleSeed;
        let zoomLevel = this.currentScale;
        zoomLevel -= this._scaleSeed;
        if (zoomLevel <= this._minScale) {
            zoomLevel = this._minScale;
            ralateScale = 1;
        }
        this.currentScale = zoomLevel;
        return ralateScale;
    }
    /**
     * 异步加载图片
     * @param callback 回调处理
     */
    public loadImageAsync (callback: (item: ImageItem) => void) {
        const _callback = (error: any, image?: HTMLImageElement) => {
            this._image = image || null;
            if (error) {
                this._isError = true;
                this._error = error;
            } else if (image) {
                this._imageWidth = image.naturalWidth;
                this._imageHeight = image.naturalHeight;
                this._imageLevel = this._imageWidth / this._imageHeight;
            }
            // 回调处理
            if (callback) {
                callback(this);
            }
        };
        this.innerLoadImageAsync(this.url, _callback);
    }
    /**
     * 内部异步加载图片
     * @param url 
     * @param callback 
     */
    private innerLoadImageAsync(url: string, callback:  (error: any, image?: HTMLImageElement) => void) {
        const _callback = (error: any, image?: HTMLImageElement) => {
            if (callback) {
                callback(error, image);
            }
        }
        const image = new Image();
        image.crossOrigin = 'Anonymous';
        image.onload = () => _callback(null, image);
        image.onerror = (error) => _callback(error);
        image.src = url;
    }
}